package com.crm.CLdriving.service.impl;

import java.util.ArrayList;
import java.util.List;


import com.baomidou.mybatisplus.plugins.Page;
import com.crm.CLdriving.common.PageResponse;
import com.crm.CLdriving.dto.PageReq.PageNumber;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.req.StudentDelectReqDto;
import com.crm.CLdriving.dto.req.StudentInsertReqDto;
import com.crm.CLdriving.dto.req.StudentSelectByIdReqDto;
import com.crm.CLdriving.dto.req.StudentSelectReqDto;
import com.crm.CLdriving.dto.req.StudentUpdateReqDto;
import com.crm.CLdriving.dto.resp.StudentSelectRespDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.StudentMapper;
import com.crm.CLdriving.po.StudentPO;
import com.crm.CLdriving.service.StudentService;
import lombok.extern.log4j.Log4j2;

@Transactional
@Service
@Log4j2
public class StudentServiceImpl implements StudentService {

	@Value("${data.pageSize.shouye}")
	private  int shouye;
	@Autowired
	private StudentMapper studentMapper;

	/**
	 * 学员信息录入 
	 * 通过身份证identity判断是否已经录入
	 */
	@Override
	public BaseResponse<?> insert(StudentInsertReqDto studentInsertReqDto) {

		StudentPO studentPO = new StudentPO();
		try {
			BeanUtils.copyProperties(studentPO, studentInsertReqDto);
			log.info("copy后："+studentPO.toString());
		} catch (Exception e) {
			log.info("copy数据失败：" + e);
		}
		EntityWrapper<StudentPO> wrapper = new EntityWrapper<>();
		wrapper.eq("identity", studentInsertReqDto.getIdentity());
		List<StudentPO> list= studentMapper.selectList(wrapper);
		if ("[]".equals(list.toString()) || list == null) {
			Integer row = studentMapper.insert(studentPO);
			if (row == 1) {
				return BaseResponse.message(ResponseEnum.SUCCESS);
			}
		}else {
			log.info("该身份信息已经录入，请更新");
			return BaseResponse.message(ResponseEnum.YJLR);
		}	
		return BaseResponse.message(ResponseEnum.FAIL);
	}


	/**
	 * 学员基本信息查询
	 */
	@Override
	public PageResponse<List<StudentSelectRespDto>> selectJB(PageNumber pageNumber) {
		Page<StudentPO> page = new Page(pageNumber.getPageNumber(),shouye);
		List<StudentPO> list = studentMapper.selectPage(page,null);
		//List<StudentPO> list = studentMapper.selectList(null);
		log.info("查询学员信息结果为："+list.toString());		
		List<StudentSelectRespDto> response = new ArrayList<StudentSelectRespDto>();	
		try {		
			for (StudentPO List : list) {
				StudentSelectRespDto selectUserResp = new StudentSelectRespDto();
				BeanUtils.copyProperties(selectUserResp, List);
				response.add(selectUserResp);
			}		
			log.info("copy后的结果为："+response.toString());
		} catch (Exception e) {
			log.info("copy异常"+e);
		}
		return PageResponse.successOfPage(response,page);
	}

	
	/**
	 * 搜索框查询
	 * 学员详细信息查询
	 */
	@Override
	public BaseResponse<List<StudentSelectRespDto>> selectXQ(StudentSelectReqDto studentSelectReqDto) {
		if(studentSelectReqDto.getName()=="" && studentSelectReqDto.getIdentityCard()==""){
			return BaseResponse.successOf(null);
		}
		List<StudentSelectRespDto> response = new ArrayList<>();
		EntityWrapper<StudentPO> wrapper = new EntityWrapper<>();
		if (studentSelectReqDto.getName() != "") {
			wrapper.like("name", studentSelectReqDto.getName());
		}
		if (studentSelectReqDto.getIdentityCard() != "") {
			wrapper.eq("identity", studentSelectReqDto.getIdentityCard());
		}
		List<StudentPO> list = studentMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			log.info("查询学员详情信息为："+list.toString());			
			for (StudentPO List : list) {
				try {
					StudentSelectRespDto selectRespDto = new StudentSelectRespDto();
					BeanUtils.copyProperties(selectRespDto, List);
					response.add(selectRespDto);
				} catch (Exception e) {
					log.info(e);
				}		
			}
		}		
		return BaseResponse.successOf(response);
	}

	
	/**
	 * 更新学员信息
	 */
	@Override
	public BaseResponse<?> updateByid(StudentUpdateReqDto studentUpdateReqDto) {
		StudentPO studentPO = new StudentPO();
		try {
			BeanUtils.copyProperties(studentPO, studentUpdateReqDto);
		} catch (Exception e) {
			log.info(""+e);
		}
		 Integer row = studentMapper.updateById(studentPO);
		 if (row == 1) {
			log.info("更新成功 update::"+row);
			return BaseResponse.message(ResponseEnum.GXCG);
		}		
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	
	/**
	 * 删除学员信息
	 */
	@Override
	public BaseResponse<?> delectByid(StudentDelectReqDto studentDelectReqDto) {
		Integer row = studentMapper.deleteById(studentDelectReqDto.getId());
		if (row == 1) {
			log.info("删除成功 delete::"+row);
			return BaseResponse.message(ResponseEnum.SQCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	/**
	 * 同过id
	 * 查询学员信息
	 */
	@Override
	public BaseResponse<StudentPO> selectByid(StudentSelectByIdReqDto studentSelectByIdReqDto) {
		StudentPO studentPO = studentMapper.selectById(studentSelectByIdReqDto.getId());
		if (studentPO != null) {
			log.info("查询信息为："+studentPO.toString());
			return BaseResponse.successOf(studentPO);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

}
